<?php
namespace Admin\Controller;
use Think\Controller;

class UserController extends Controller
{
    private $user;
    public function __construct()
    {
        parent::__construct();
        $this->user=D('User');
    }

    public function register()
    {
        $this->display();
    }

    public function do_register()
    {
        if(!$this->user->create()){
            $this->error($this->user->getError());
        }else{
            $this->user->add();
            $this->success('注册成功',U('login'));
        }
    }

    public function login(){
        $this->display();
    }

    public function do_login(){
        $code = I('post.code');
        if(!$this->check_verify($code)){
            $this->error('验证码不正确！');
        }

        $data['email']=trim(I('post.email'));
        $data['password']=substr(md5(I('post.password')),5,-5);
        $user=$this->user->where($data)->find();
        if(!$user){
            $this->error('用户或密码错误！');
        }else{
            session('user',$user);
            if(I('post.rem')){
                setcookie('token',$user['token'],time()+3600,'/');
                //cookie('token',$user['token'],3600);
                //cookie('token',$user['token'],array('expire'=>3600));
            }
            //dump($_SESSION);

            $this->success('登录成功',U('Admin/Index/index'));
        }
    }

    public function logout(){
        //echo session_name();
        //exit();

        $_SESSION=array();
        /*setcookie('PHPSESSID','',time()-3600);
        setcookie('token','',time()-3600);
        session('[destroy]');*/

        if(isset($_COOKIE[session_name()])){
            cookie(session_name(),null);
            cookie('token',null);
        }
        $this->success("退出成功",U('login'));
    }

    public function verify(){
        $Verify = new \Think\Verify(C('verify'));
        $Verify->entry();
    }
    function check_verify($code, $id = ''){
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }
}